//
//  PKRequestManager.m
//  Pianke
//
//  Created by lanou on 15/12/22.
//  Copyright © 2015年 Huang. All rights reserved.
//

#import "FMDB.h"
#import "PKRequestManager.h"

@implementation PKRequestManager

+ (void)requestWithUrlString:(NSString *)urlString parDic:(NSDictionary *)parDic appendingString:(NSString *)appdingString method:(RequestType)method finish:(Finish)finish error:(Error)error {
    // url字符串转化为url对象
    NSURL* url = [NSURL URLWithString:[[NetStr stringByAppendingString: appdingString]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

    // 用url对象初始化请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:8];

    // 判断请求方式是否为POST请求,否则默认为GET(GET不用设置请求体)
    if (method == POST) {
        // 设置请求方式
        [request setHTTPMethod:@"POST"];
        // 判断字典中是否有键值对
        if (parDic.count != 0) {
            // 初始化一个字典用来后面保存格式化的字符串键值对
            NSMutableArray *strArray = [NSMutableArray array];
            // 遍历字典
            for (NSString *key in parDic) {
                // 以key=value形式存储键值对于数组中
                NSString *str = [NSString stringWithFormat:@"%@=%@",key,parDic[key]];
                [strArray addObject:str];
            }
            // 所有数组中的元素一次性以某个字符串拼接成另一个字符串
            NSString *parString = [[strArray componentsJoinedByString:@"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            // 设置请求体
            [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
        }
    }
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            NSLog(@"请求失败");
            error(connectionError);
            
            
        }
    }];


  
}


+(void)AddRemote_DataWithnetUrl:(NSString*)netUrl userID:(NSString*)userID UploadWay:(NSInteger)UploadWay zipData:(NSString*)zipData method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/Data/AddRemote_Data"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@",userID];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"UploadWay=%ld",(long)UploadWay];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"zipData=%@",zipData];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)GetDeviceInfoWithnetUrl:(NSString*)netUrl deviceID:(NSString*)deviceID method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/Device/GetDeviceInfo"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"deviceID=%@",deviceID];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
    [request setValue: token forHTTPHeaderField:@"Authorization"];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)CheckUserDeviceWithnetUrl:(NSString*)netUrl userID:(NSString*)userID iDCode:(NSString*)iDCode method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/Device/CheckUserDevice"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@",userID];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"iDCode=%@",iDCode];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}

+(void)ChangeDeviceUserIDWithnetUrl:(NSString*)netUrl userID:(NSString*)userID iDCode:(NSString*)iDCode password:(NSString*)password oldpassword:(NSString*)oldpassword method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/Device/ChangeDeviceUserID"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@",userID];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"iDCode=%@",iDCode];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"password=%@",password];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"oldpassword=%@",oldpassword];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
    [request setValue: token forHTTPHeaderField:@"Authorization"];
    

    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)GetStaticPixelWithnetUrl:(NSString*)netUrl brand:(NSString*)brand model:(NSString*)model method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/Static/GetStaticPixel"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"brand=%@",brand];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"model=%@",model];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)GetPreSetMd5WithnetUrl:(NSString*)netUrl  method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/Static/GetPreSetMd5"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)GetPreSetUrlsWithnetUrl:(NSString*)netUrl  method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/Static/GetPreSetUrls"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)GetCurVerWithnetUrl:(NSString*)netUrl platform:(NSInteger)platform method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/Static/GetCurVer"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"platform=%ld",(long)platform];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)RegistWithnetUrl:(NSString*)netUrl loginName:(NSString*)loginName password:(NSString*)password validCode:(NSString*)validCode IDCard:(NSString*)IDCard method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    
    
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/User/Regist"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    
//    NSURL* url = [NSURL URLWithString:[[@"http://192.168.1.134:56285" stringByAppendingString: @"/api/Data/AddRemote_Data"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"loginName=%@",loginName];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"password=%@",password];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"validCode=%@",validCode];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"IDCard=%@",IDCard];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}


+(void)ChangePwdWithnetUrl:(NSString*)netUrl userID:(NSString*)userID password:(NSString*)password passwordOld:(NSString*)passwordOld method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/User/ChangePwd"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@",userID];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"password=%@",password];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"passwordOld=%@",passwordOld];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
        [request setValue: token forHTTPHeaderField:@"Authorization"];

    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            NSLog(@"失败");
            error(connectionError);
        }
    }];
}

+(void)ChangePwdWithnetUrl:(NSString*)netUrl upLoadWay:(NSString *)uploadWay userID:(NSString*)userID deviceID:(NSString *)deviceID filenames:(NSArray *)filenames data:(NSData *)fileData method: (RequestType)method finish: (Finish)finish  error: (Error)error{

    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/data/AddRemote_Data"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    
         NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    
    
    
    
    //分界线 --AaB03x
    NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
    NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
    
    //结束符 AaB03x--
    NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
    

    if (method == POST)
    {

        
//        获取数组里面的所有文件 写进data
//        
//        NSString *path = [[NSBundle mainBundle] pathForResource:@"20160510103203.zip" ofType:nil];
        NSMutableArray *arr = [NSMutableArray array];

//        NSURL *url2 = [NSURL fileURLWithPath:path];
        
//        NSData *filedata = [NSData dataWithContentsOfURL:url2];
     
        //            FMDB 是根据路径操作的 所以先找到路径
        NSString *nameList = [NSString stringWithFormat:@"ECGDatalist.db"];
        
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:nameList];
        
        //  根据路径去创建一个数据库
        FMDatabase *db = [FMDatabase databaseWithPath:path];
        
        
        if ([db open]) {
            NSLog(@"数据库打开成功");
        }else
        {
            NSLog(@"数据库打开失败");
        }
        
        for (int i = 0; i < filenames.count; i ++) {
            
//            根据fileName 找到数据库里面表的信息
            NSString *BeginDT = @"";
             NSString *EndDT = @"";
            NSString *fileName = filenames[i] ;

            if (fileName.length > 12) {
                fileName = [ fileName substringToIndex:12];
            }
            FMResultSet *rs = [db executeQuery:@"SELECT * FROM APPInformationlist where FileName = ?",fileName];
            while ([rs next]) {
               BeginDT = [rs stringForColumn:@"BeginDT"];
                EndDT  = [rs stringForColumn:@"EndDT"];
                
            }
            if (EndDT == nil || [EndDT isEqualToString:@""]) {
                EndDT = BeginDT;
            }
            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            dic[@"UserID"] = userID;
            dic[@"DeviceID"] = deviceID;
            dic[@"BeginDT"] = BeginDT;
            dic[@"EndDT"] = EndDT;
            dic[@"FileName"] = filenames[i];
            
            NSDictionary *dict = [NSDictionary dictionaryWithDictionary:dic];
            [arr addObject:dict];
        }
        NSMutableArray *strArray = [NSMutableArray array];
        for (int i = 0; i < arr.count; i++) {
            NSString *str = [self dictionaryToJson:arr[i]];
            [strArray addObject:str];
        }
        NSString* parString = [strArray componentsJoinedByString: @","];
        NSString *listString = [NSString stringWithFormat:@"[%@]",parString];

        NSMutableString *mutableString = [[NSMutableString alloc]init];
        
//        第一个参数  userID
        mutableString = [self setParamsKey:@"userID" value:userID body:mutableString];
//        第二个参数
        mutableString = [self setParamsKey:@"desDataJson" value:listString body:mutableString];
//        第三个参数
        mutableString = [self setParamsKey:@"upLoadWay" value:uploadWay body:mutableString];
        
#pragma mark 添加文件流
//        添加分界线，换行
        [mutableString appendFormat:@"%@\r\n",MPboundary];
        //声明文件字段，文件名
        [mutableString appendFormat:@"Content-Disposition: form-data; name=\"upfile\"; filename=\"%@\"\r\n",@"20160510103203.zip"];
        
        //声明上传文件的格式
        [mutableString appendFormat:@"Content-Type: %@\r\n\r\n",@"application/octet-stream"];

        //声明结束符：--AaB03x--
        NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];
        
        //声明myRequestData，用来放入http body
        NSMutableData *myRequestData=[NSMutableData data];
        
        //将body字符串转化为UTF8格式的二进制
        [myRequestData appendData:[mutableString dataUsingEncoding:NSUTF8StringEncoding]];
        
        //将file的data加入
        [myRequestData appendData:fileData];
        
        //加入结束符--AaB03x--
        [myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
        
#pragma mark  至此 上传的data 完毕
        //设置HTTPHeader中Content-Type的值
        NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
        
        //设置HTTPHeader
        [request setValue:content forHTTPHeaderField:@"Content-Type"];
        //设置Content-Length
        [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[myRequestData length]] forHTTPHeaderField:@"Content-Length"];
        
        //设置http body
        [request setHTTPBody:myRequestData];

        //http method
        [request setHTTPMethod:@"POST"];
    }
    NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
    [request setValue: token forHTTPHeaderField:@"Authorization"];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用

            finish(data);
        } else {// 请求失败后block调用
            NSLog(@"失败");
            error(connectionError);
        }
    }];
}
//批量上传
+(void)uploadPiliangWithnetUrl:(NSString*)netUrl userID:(NSString*)userID deviceID:(NSString *)deviceID filenames:(NSString *)filename data:(NSData *)fileData method: (RequestType)method finish: (Finish)finish  error: (Error)error{
    
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/data/AddRemote_Data"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    //分界线 --AaB03x
    NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
    NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
    
    //结束符 AaB03x--
    NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
    
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        
        
        //        获取数组里面的所有文件 写进data
        //
        //        NSString *path = [[NSBundle mainBundle] pathForResource:@"20160510103203.zip" ofType:nil];
        NSMutableArray *arr = [NSMutableArray array];
        
        //        NSURL *url2 = [NSURL fileURLWithPath:path];
        
        //        NSData *filedata = [NSData dataWithContentsOfURL:url2];
        
        //            FMDB 是根据路径操作的 所以先找到路径
        NSString *nameList = [NSString stringWithFormat:@"ECGDatalist.db"];
        
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:nameList];
        
        //  根据路径去创建一个数据库
        FMDatabase *db = [FMDatabase databaseWithPath:path];
        
        
        if ([db open]) {
            NSLog(@"数据库打开成功");
        }else
        {
            NSLog(@"数据库打开失败");
        }
        
        
            //            根据fileName 找到数据库里面表的信息
            NSString *BeginDT = @"";
            NSString *EndDT = @"";
        filename = [ filename substringToIndex:12];

            FMResultSet *rs = [db executeQuery:@"SELECT * FROM APPInformationlist where FileName = ?",filename];
            while ([rs next]) {
                BeginDT = [rs stringForColumn:@"BeginDT"];
                EndDT = [rs stringForColumn:@"EndDT"];
            }
            
            
            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            dic[@"UserID"] = userID;
            dic[@"DeviceID"] = deviceID;
            dic[@"BeginDT"] = BeginDT;
            dic[@"EndDT"] = EndDT;
            dic[@"FileName"] = filename;
            
            NSDictionary *dict = [NSDictionary dictionaryWithDictionary:dic];
            [arr addObject:dict];

        
        
        
        NSMutableArray *strArray = [NSMutableArray array];
        for (int i = 0; i < arr.count; i++) {
            NSString *str = [self dictionaryToJson:arr[i]];
            [strArray addObject:str];
        }
        NSString* parString = [strArray componentsJoinedByString: @","];
        NSString *listString = [NSString stringWithFormat:@"[%@]",parString];
        
        NSMutableString *mutableString = [[NSMutableString alloc]init];
        
        //        第一个参数  userID
        mutableString = [self setParamsKey:@"userID" value:userID body:mutableString];
        //        第二个参数
        mutableString = [self setParamsKey:@"desDataJson" value:listString body:mutableString];
        //        第三个参数
        mutableString = [self setParamsKey:@"upLoadWay" value:@"1" body:mutableString];
        
#pragma mark 添加文件流
        //        添加分界线，换行
        [mutableString appendFormat:@"%@\r\n",MPboundary];
        //声明文件字段，文件名
        [mutableString appendFormat:@"Content-Disposition: form-data; name=\"upfile\"; filename=\"%@\"\r\n",@"20160510103203.zip"];
        
        //声明上传文件的格式
        [mutableString appendFormat:@"Content-Type: %@\r\n\r\n",@"application/octet-stream"];
        
        //声明结束符：--AaB03x--
        NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];
        
        //声明myRequestData，用来放入http body
        NSMutableData *myRequestData=[NSMutableData data];
        
        //将body字符串转化为UTF8格式的二进制
        [myRequestData appendData:[mutableString dataUsingEncoding:NSUTF8StringEncoding]];
        
        //将file的data加入
        [myRequestData appendData:fileData];
        
        //加入结束符--AaB03x--
        [myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
        
#pragma mark  至此 上传的data 完毕
        //设置HTTPHeader中Content-Type的值
        NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
        
        //设置HTTPHeader
        [request setValue:content forHTTPHeaderField:@"Content-Type"];
        //设置Content-Length
        [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[myRequestData length]] forHTTPHeaderField:@"Content-Length"];
        
        //设置http body
        [request setHTTPBody:myRequestData];
        
        //http method
        [request setHTTPMethod:@"POST"];
    }
    NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
    [request setValue: token forHTTPHeaderField:@"Authorization"];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            
            finish(data);
        } else {// 请求失败后block调用
            NSLog(@"失败");
            error(connectionError);
        }
    }];
}
#warning mark  设置上传
/****************************************************/

+(void)UpdateUserBaseWithnetUrl:(NSString*)netUrl userID:(NSString*)userID iDCard:(NSString*)iDCard height:(NSNumber*)height weight:(NSNumber*)weight zipCode:(NSString*)zipCode realName:(NSString*)realName remark:(NSString*)remark fullAddr:(NSString*)fullAddr moblieNum:(NSString*)moblieNum method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/User/UpdateUserBase"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@",userID];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"iDCard=%@",iDCard];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"height=%@",height];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"weight=%@",weight];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"zipCode=%@",zipCode];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"realName=%@",realName];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"remark=%@",remark];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"fullAddr=%@",fullAddr];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"moblieNum=%@",moblieNum];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
        [request setValue: token forHTTPHeaderField:@"Authorization"];

    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)AddUserFamilyWithnetUrl:(NSString*)netUrl userID:(NSString*)userID loginName:(NSString*)loginName fName:(NSString*)fName method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/User/AddUserFamily"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@",userID];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"loginName=%@",loginName];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"fName=%@",fName];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
        
        NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
        [request setValue: token forHTTPHeaderField:@"Authorization"];

        
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)DeleteUserFamilyWithnetUrl:(NSString*)netUrl userID:(NSString*)userID iD:(NSInteger)iD method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/User/DeleteUserFamily"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@",userID];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"iD=%ld",(long)iD];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
        [request setValue: token forHTTPHeaderField:@"Authorization"];
        

    }
    
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)ForgetPwdWithnetUrl:(NSString*)netUrl longName:(NSString*)longName vaildCode:(NSString*)vaildCode password:(NSString*)password method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/User/ForgetPwd"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    
   

    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"longName=%@",longName];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"vaildCode=%@",vaildCode];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"password=%@",password];
        [strArray addObject:str];
        
        
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
        [request setValue: token forHTTPHeaderField:@"Authorization"];
        

    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}

+(void)GetMyFamilyWithnetUrl:(NSString*)netUrl userID:(NSString*)userID method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/User/GetMyFamily"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@",userID];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
        
        NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
        [request setValue: token forHTTPHeaderField:@"Authorization"];

        
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)GetMyWearerWithnetUrl:(NSString*)netUrl userID:(NSString*)userID method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/User/GetMyWearer"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@",userID];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)GetRegisterVerifyCodeWithnetUrl:(NSString*)netUrl loginName:(NSString*)loginName method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/User/GetRegisterVerifyCode"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"loginName=%@",loginName];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用sl
    
            NSLog(@"请求失败");
            error(connectionError);
        }
    }];
}
+(void)GetForgetVerifyCodeWithnetUrl:(NSString*)netUrl moblieNo:(NSString*)moblieNo method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/User/GetForgetVerifyCode"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"moblieNo=%@",moblieNo];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)InserGPSWithnetUrl:(NSString*)netUrl userID:(NSString*)userID gPSLo:(NSString*)gPSLo gPSLa:(NSString*)gPSLa method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/User/InserGPS"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@",userID];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"gPSLo=%@",gPSLo];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"gPSLa=%@",gPSLa];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
        
        NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
        [request setValue: token forHTTPHeaderField:@"Authorization"];

        
        
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) { 
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}
+(void)GetUserBaseWithnetUrl:(NSString*)netUrl userID:(NSString*)userID method: (RequestType)method finish: (Finish)finish  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[netUrl stringByAppendingString: @"/api/User/GetUserBase"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@",userID];
        [strArray addObject:str];
        
        
        
        
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
        
        
        NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
        [request setValue: token forHTTPHeaderField:@"Authorization"];
    }
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) { 
        if (nil == connectionError) {// 请求成功后block调用
            finish(data);
        } else {// 请求失败后block调用
            error(connectionError);
        }
    }];
}

+(void)GetFreeDeviceVerifyWithuserID:(NSString*)userID iDCode:(NSString*)iDCode method: (RequestType)method finish: (Finish)finish string:(NSString *)urlString  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[urlString stringByAppendingString: @"/api/Device/GetFreeDeviceVerify"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@", userID];
        
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"iDCode=%@", iDCode];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
        [request setValue: token forHTTPHeaderField:@"Authorization"];
    }
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
            if (nil == connectionError) {// 请求成功后block调用
                finish(data);
            } else {// 请求失败后block调用
                error(connectionError);
            }
        }];
    }

+(void)FreeDeviceWithuserID:(NSString*)userID iDCode:(NSString*)iDCode verifyCode:(NSString*)verifyCode method: (RequestType)method finish: (Finish)finish string:(NSString *)urlString  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[urlString stringByAppendingString: @"/api/Device/FreeDevice"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@", userID];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"iDCode=%@", iDCode];
        [strArray addObject:str];
        str = [NSString stringWithFormat: @"verifyCode=%@", verifyCode];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
    [request setValue: token forHTTPHeaderField:@"Authorization"];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
    if (nil == connectionError) {// 请求成功后block调用
        finish(data);
    } else {// 请求失败后block调用
        error(connectionError);
    }
}];
}


+(void)sendRequest: (NSURLRequest*)request finish: (Finish)finish error: (Error)error {
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]
                           completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
                               if (nil == connectionError) {// 请求成功后block调用
                                   NSLog(@"请求成功");
                                   finish(data); 
                               } else {// 请求失败后block调用
                                   NSLog(@"请求失败");
                                   error(connectionError);
                               } 
                           }];
}

//获取用户的设备
+(void)GetUserDeviceWithuserID:(NSString*)userID method: (RequestType)method finish: (Finish)finish string:(NSString *)urlString  error: (Error)error {
    NSURL* url = [NSURL URLWithString:[[urlString stringByAppendingString: @"/api/Device/GetUserDevice"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
    if (method == POST)
    {
        [request setHTTPMethod:@"POST"];
        NSMutableArray* strArray = [NSMutableArray array];
        NSString* str=@"";
        str = [NSString stringWithFormat: @"userID=%@",userID];
        [strArray addObject:str];
        NSString* parString = [[strArray componentsJoinedByString: @"&"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:[parString dataUsingEncoding:NSUTF8StringEncoding]];
 
    
    NSString *token = [NSString stringWithFormat:@"Bearer %@",[BSUserSGton userHandle].user_token];
    [request setValue: token forHTTPHeaderField:@"Authorization"];
    

    }
      [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue]   completionHandler:^(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError) {
    if (nil == connectionError) {// 请求成功后block调用
        finish(data);
    } else {// 请求失败后block调用
        error(connectionError);
    }
}];

}

+ (void)showAlertView:(NSString *)str{
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    
}



//<?xml version="1.0" encoding="utf-8"?>
//<ResponseCodes>
//<ResponseCode Code="-999" Msg="服务异常" />
//<ResponseCode Code="0" Msg="操作成功" />
//<ResponseCode Code="3" Msg="操作无效" />
//<ResponseCode Code="1000" Msg="设备不存在" />
//<ResponseCode Code="1001" Msg="密码不正确" />
//<ResponseCode Code="1002" Msg="设备已经被使用" />
//<ResponseCode Code="1003" Msg="设备的使用人不正确" />
//<ResponseCode Code="1004" Msg="设备未启用" />
//<ResponseCode Code="1005" Msg="设备已关闭" />
//<ResponseCode Code="1006" Msg="更换设备" />
//<ResponseCode Code="2000" Msg="登录人员不存在" />
//<ResponseCode Code="2001" Msg="用户名或密码不正确" />
//<ResponseCode Code="2002" Msg="登录手机号已经存在" />
//<ResponseCode Code="2003" Msg="超出最大家属限制" />
//<ResponseCode Code="2004" Msg="注册超出最大短信次数" />
//<ResponseCode Code="2005" Msg="身份证信息错误" />
//<ResponseCode Code="2006" Msg="原密码不正确" />
//<ResponseCode Code="2007" Msg="超出最大短信次数" />
//<ResponseCode Code="3000" Msg="验证码失效，或验证码不正确" />
//<ResponseCode Code="9001" Msg="手机型号不存在" />
//</ResponseCodes>




//+ (void)resultOfTheCode:(NSString *)code{
//
//    if ([code isEqualToString:@"-999"]) {
//        [self showAlertView:@"服务异常"];
//    } else if([code isEqualToString:@"3"]) {
//        [self showAlertView:@"操作无效"];
//    }else if([code isEqualToString:@"1000"]) {
//       [self showAlertView:@"设备不存在"];  ;
//    }else if([code isEqualToString:@"1001"]) {
//         [self showAlertView:@"密码不正确"];;
//    }else if([code isEqualToString:@"1002"]) {
//         [self showAlertView:@"设备已经被使用"];;
//    }else if([code isEqualToString:@"1003"]) {
//         [self showAlertView:@"设备的使用人不正确"];;
//    }else if([code isEqualToString:@"1004"]) {
//         [self showAlertView:@"设备未启用"];;
//    }else if([code isEqualToString:@"1005"]) {
//         [self showAlertView:@"设备已关闭"];;
//    }else if([code isEqualToString:@"1006"]) {
//         [self showAlertView:@"更换设备"];;
//    }else if([code isEqualToString:@"2000"]) {
//         [self showAlertView:@"登录人员不存在"];;
//    }else if([code isEqualToString:@"2001"]) {
//         [self showAlertView:@"用户名或密码不正确"];;
//    }else if([code isEqualToString:@"2002"]) {
//         [self showAlertView:@"登录手机号已经存在"];;
//    }else if([code isEqualToString:@"2003"]) {
//         [self showAlertView:@"超出最大家属限制"];;
//    }else if([code isEqualToString:@"2004"]) {
//         [self showAlertView:@"注册超出最大短信次数"];;
//    }else if([code isEqualToString:@"2005"]) {
//         [self showAlertView:@"身份证信息错误"];;
//    }else if([code isEqualToString:@"2006"]) {
//         [self showAlertView:@"原密码不正确"];;
//    }else if([code isEqualToString:@"2007"]) {
//         [self showAlertView:@"超出最大短信次数"];;
//    }else if([code isEqualToString:@"3000"]) {
//         [self showAlertView:@"验证码失效，或验证码不正确"];;
//    }else if([code isEqualToString:@"9001"]) {
//         [self showAlertView:@"手机型号不存在"];;
//    }
//
//
//}

+ (NSString*)dictionaryToJson:(NSDictionary *)dic

{
    
    NSError *parseError = nil;
    
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}
+ (NSMutableString*)setParamsKey:(NSString*)key value:(NSString*)value body:(NSMutableString*)body{
    
    NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
    
    
    //分界线 --AaB03x
    NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
    
    //添加分界线，换行

    [body appendFormat:@"%@\r\n",MPboundary];
    
    //添加字段名称，换2行
    
    [body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
    

    //添加字段的值
    
    [body appendFormat:@"%@\r\n",value];
    
    return body;
    
}



@end
